package com.jike.noobmoney.mvp.view.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.ShenHeAdapter;
import com.jike.noobmoney.adapter.ShenShuAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.ShenHeEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.mvp.view.activity.ShenHeDetailActivity;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.rxbus.RxBusRoute;
import com.jike.noobmoney.rxbus.RxEvent;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 审核
 */
public class ShenShuFragment extends BaseNewFragment implements IView {

    public static String Channels = "mypush";
    public static String t_id = "t_id";

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    ImageView empty;


    private String status = "";

    private ShenShuAdapter adapter;
    private List<ShenHeEntity.OrderlistBean> mData = new ArrayList<>();
    private TaskPresenter taskPresenter;
    private int page = 1;
    private boolean isRefresh = true;
    private String taskId;


    @Override
    protected int layoutResID() {
        return R.layout.fragment_shen_he;
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        taskPresenter = new TaskPresenter(this);
        initData();
    }

    protected void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            status = bundle.getString(ShenShuFragment.Channels);
            taskId = bundle.getString(ShenShuFragment.t_id);
        }

        getData(true);

        //adapter
        adapter = new ShenShuAdapter(context);
        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(refreshlayout -> {
            getData(true);
        });

//        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
//            getData(false);
//        });

        adapter.setOnItemClickListener((adapterView, view, position, l) -> {
            ShenHeEntity.OrderlistBean item = (ShenHeEntity.OrderlistBean) adapter.getItem(position);
            if (item.getStatus() == 1) {
                //待完成 不让看详情
                return;
            }
            ShenHeDetailActivity.startAct(context, "" + item.getO_id(),status);

        });

    }

    @Override
    protected boolean isRegistRxBus() {
        return true;
    }

    protected void receiveEvent(RxEvent rxEvent) {
        switch (rxEvent.busName) {
            case RxBusRoute.SHEHE:
                if ("3".equals(status)) {
                    getData(true);
                }
                break;
        }
    }

    /**
     * 获取数据
     */
    private void getData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        showProgressDialog();
        if (taskPresenter != null) {
            taskPresenter.getShenHeList(taskId, status, "" + page, "10", ConstantValue.RequestKey.shenhe_list);
        }
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        disLoading();
        if (ConstantValue.RequestKey.shenhe_list.equals(actionType)) {
            List<ShenHeEntity.OrderlistBean> orderlist = (List<ShenHeEntity.OrderlistBean>) data;
            mData.clear();
            if (orderlist != null && orderlist.size() > 0) {
//                if (isRefresh) {
//                    mData.clear();
//                    mData.addAll(orderlist);
//                } else {
//                    mData.addAll(orderlist);
//                }

                mData.addAll(orderlist);
            }

            if (empty != null) {
                if (mData.size() > 0) {
                    empty.setVisibility(View.GONE);
                } else {
                    empty.setVisibility(View.VISIBLE);
                }
            }
            adapter.refresh(mData);
        }


    }

    @Override
    public void onFailed(String code, String msg) {
        disLoading();
    }

    private void disLoading() {
        dismissProgressDialog();
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }


    }
}
